package com.clgc.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.clgc.eduservice.entity.EduTopic;
import com.clgc.eduservice.entity.excel.TopicExcel;
import com.clgc.eduservice.listener.ExcelListener;
import com.clgc.eduservice.mapper.EduTopicMapper;
import com.clgc.eduservice.service.EduCourseService;
import com.clgc.eduservice.service.EduTopicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clgc.eduservice.entity.vo.TaskVo;
import com.clgc.eduservice.schedule.TopicSche;
import com.clgc.utils.MyListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 题库 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-04-27
 */
@Service
public class EduTopicServiceImpl extends ServiceImpl<EduTopicMapper, EduTopic> implements EduTopicService {

    @Autowired
    EduCourseService courseService;

    @Autowired
    TopicSche topicSche;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveFile(MultipartFile file, EduTopicService topicService) {
        try {
            InputStream inputStream = file.getInputStream();

            EasyExcel.read(inputStream, TopicExcel.class, new ExcelListener(topicService, courseService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<EduTopic> getTopicByNum(TaskVo vo) {
        if (vo != null) {
            List<EduTopic> list = new ArrayList<>();
            if (vo.getChoiceNum() > 0) {
                List<EduTopic> choiceList = topicSche.getChoice();
                MyListUtils.GetRandomThreeInfoList(choiceList, vo.getChoiceNum()).forEach(topic -> {
                    EduTopic topic1 = (EduTopic) topic;
                    list.add(topic1);
                });
            }
            if (vo.getMultipleNum() > 0) {
                List<EduTopic> multipleList = topicSche.getMultipleNum();
                MyListUtils.GetRandomThreeInfoList(multipleList, vo.getMultipleNum()).forEach(topic -> {
                    EduTopic topic1 = (EduTopic) topic;
                    list.add(topic1);
                });
            }
            if (vo.getFillNum() > 0) {
                List<EduTopic> fillList = topicSche.getFillNum();
                MyListUtils.GetRandomThreeInfoList(fillList, vo.getFillNum()).forEach(topic -> {
                    EduTopic topic1 = (EduTopic) topic;
                    list.add(topic1);
                });
            }
            if (vo.getJudgmentNum() > 0) {
                List<EduTopic> judgmentList = topicSche.getJudgmentNum();
                MyListUtils.GetRandomThreeInfoList(judgmentList, vo.getChoiceNum()).forEach(topic -> {
                    EduTopic topic1 = (EduTopic) topic;
                    list.add(topic1);
                });
            }
            if (vo.getShortNum() > 0) {
                List<EduTopic> shortList = topicSche.getShortNum();
                MyListUtils.GetRandomThreeInfoList(shortList, vo.getChoiceNum()).forEach(topic -> {
                    EduTopic topic1 = (EduTopic) topic;
                    list.add(topic1);
                });
            }
            return list;
        }

        return null;
    }

}
